<template>
    <div class="col-3">
        <el-button @click="create1" type="primary">+团队地址</el-button>
        <el-dialog width="60%" :title="title" @close="() => close()" v-model="dialogVisible">
            <template #header>
                <span class="icon-text el-dialog__header-title">
                    <el-icon class="text-primary">
                        <Guide />
                    </el-icon>
                    {{ title }}
                </span>
            </template>
            <el-form size="default" :model="formdata" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="国家">
                    <el-input v-model="formdata.country" placeholder="国家" />
                </el-form-item>
                <el-form-item label="省">
                    <el-input v-model="formdata.province" placeholder="省" />
                </el-form-item>
                <el-form-item label="城市">
                    <el-input v-model="formdata.city" placeholder="城市" />
                </el-form-item>
                <el-form-item label="区">
                    <el-input v-model="formdata.area" placeholder="区" />
                </el-form-item>
                <el-form-item label="街道">
                    <el-input v-model="formdata.street" placeholder="街道" />
                </el-form-item>
                <el-form-item label="经度">
                    <el-input v-model="formdata.longitude" placeholder="经度" />
                </el-form-item>
                <el-form-item label="维度">
                    <el-input v-model="formdata.dimension" placeholder="维度" />
                </el-form-item>
                <el-form-item label="详细地址">
                    <el-input v-model="formdata.home" placeholder="详细地址" />
                </el-form-item>
                <el-form-item label="学校">
                    <el-input v-model="formdata.school" placeholder="学校" />
                </el-form-item>
                <el-form-item label="组织">
                    <el-input v-model="formdata.organization" placeholder="组织" />
                </el-form-item>
                <el-form-item label="公司">
                    <el-input v-model="formdata.company" placeholder="公司" />
                </el-form-item>
                <el-form-item label="个人">
                    <el-input v-model="formdata.user" placeholder="个人" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { computed, reactive, ref, watch } from "vue";
import { asynccreateorupdateteamhome } from "../../../api";
import { default as Studycodemodel, default as study } from "../../../mysql_interface/studycodemodel";

const p = defineProps<{ teamhome: Studycodemodel.Iteamhome, ID?: number, close: Function, teamid?: number, start?: Function }>()

const formdata = reactive<study.Iteamhome>({ ...p.teamhome });
const dialogVisible = ref(false);

const title = computed(() => {
    if (p.ID) {
        return "修改";
    }
    return "创建";
});

watch(() => p.teamhome, () => {
    Object.assign(formdata, p.teamhome);
}
);

async function submit() {

    formdata.teamid = Number(p.teamid);
    try {
        await asynccreateorupdateteamhome(formdata);
        ElMessage.success(title.value + "成功");
        dialogVisible.value = false;
        if (p.start) p.start();
    } catch (e: any) {
        ElMessage.error(e);
    }
}
async function create1() {
    dialogVisible.value = true;
}

defineExpose({
    setdialogVisible: create1
})

</script>